# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-TODAY OpenERP S.A. <http://www.openerp.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.addons.dm_base.utils import field_boolean_name_cn as boolean_name,field_selection_name as selection_name
from openerp.addons.dm_rubylong import get_rubylong_fields_xml,get_rublong_data_url
from openerp.osv import fields, osv
from datetime import datetime
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT

class stock_batch(osv.osv):
    
    _inherit="stock.batch"
    
    def _rubylong_xml_file(self, cr, uid, ids, field_names, args, context=None):
        if isinstance(ids, (int, long)):
            ids = [ids]
        res = {}
        for order_id in ids:
            res[order_id] = ''
        data_xml = ''
        for order in self.browse(cr, uid, ids, context=context):
            #当前对象的xml
            data_xml = ''
            #header data
            order_fields = [
                        ('company_id.logo','company_logo'),
                        ('company_id.name','company_id_name'),
                        #单据编号
                        'name',
                        #采购单位
                        ('partner_id.code','code'),
                        ('partner_id.name','partner_id'),
                        #制单人
                        ('create_uid.name','create_uid'),
                        #入库日期
                        'date_order',
                        'date_done',
                        
                        ('id','order_id'),    
                        ]
#            #入库日期
#            date_done = order.date_done and (fields.datetime.context_timestamp(cr, uid,
#                                                        timestamp=datetime.strptime(order.date_done, DEFAULT_SERVER_DATETIME_FORMAT),
#                                                        context=context)) or ''
#                        
            #总收货金额
            amount_total = 0.0
            for line in order.order_line:
                amount_total += line.move_id.price_subtotal_order
            order_fields_new = order_fields + [{'amount_total':amount_total}]
            if order.warehouser_id:
                order_fields_new.append({'done_user_id':order.warehouser_id.name})
            data_xml += get_rubylong_fields_xml(order, 'header', order_fields_new)
            
            #detail data
            line_fields = [
                        ('move_id.location_id.name','location_id'),
                        ('move_id.location_dest_id.name','location_dest_id'),
                        #物料编码
                        ('move_id.product_id.default_code','default_code'),
                        ('move_id.product_id.name','product_id'),
                        #规格
                        ('move_id.product_id.variants','variants'),
                        #单位
                        ('move_id.product_uom.name','product_uom'),
                        #数量
                        ('move_id.product_qty','product_qty'),
                        #单价
                        ('move_id.price_order','price_unit'),
                        #金额
                        ('move_id.price_subtotal_order','amount'),
                        ('move_id.product_id.part_no_external','part_no_extermal'),
                        ('picking_id.id','order_id'),
                        ('product_id.part_no_external','part_no_extermal'),  
                        #供应商&客户名称
                        ('move_id.supplier_prod_name','supplier_prod_name'),
                        ('move_id.customer_prod_name','customer_prod_name')
                        ] 
            order.order_line.sort(key=lambda x:x.move_id.product_id.default_code)
                    
            seq = 0
            for line in order.order_line:
                seq += 1
                if line.move_id.state == 'cancel':
                    continue
                #获得订单ID
                purchase_id = line.picking_id.purchase_id and line.picking_id.purchase_id.name or ''
                sale_id = line.picking_id.sale_id and line.picking_id.sale_id.name or ''
                production_id = line.picking_id.production_id and line.picking_id.production_id.name or ''
                line_fields_new = line_fields + [{'order_line':seq}, {'purchase_id':purchase_id}, {'sale_id':sale_id}, {'production_id':production_id}]
                data_xml += get_rubylong_fields_xml(line, 'detail', line_fields_new)

            #write data to file            
            res[order.id] = get_rublong_data_url(order,data_xml,cr.dbname)
            
        return res    

    _columns = {
            'rubylong_xml_file':fields.function(_rubylong_xml_file, type='text', string='Rubylong xml'),
            }
#po_super.STATE_SELECTION = STATE_SELECTION_PO  